<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :table-name="tableName"
    :form-items="formItems"
    :form-data="formData"
    :table-id="tableId"
    :before-save="beforeSave"
    @success="onSuccess"
    @close="handleClose"
    :submitText="submitText"
    label-width="140px"
    :title="title"
  ></c-form-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { phoneValidator, idCardValidator, registerNoValidator } from '@/common/plugins/validator'
import { enumCompanyType, enumCompanyLevel } from '@/gov/plugins/enum'

import SelectAddress from '@/components/select/SelectAddress'
// import SelectWarehouseVue from '@/gov/components/select/SelectWarehouse.vue'
import SelectCustomer, { fields } from '@/gov/components/select/SelectCustomer'
export default {
  props: {
    visible: Boolean,
    isFinished: {
      type: Boolean,
      default: false,
    },
    packageId: {
      type: String,
      default: '',
    },
    packageName: {
      type: String,
      default: '',
    },
    formData: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    const { packageId } = this
    return {
      innerVisible: false,
      tableName: 'project_white_list',
      formItems: [
        {
          tag: SelectCustomer,
          itemAttrs: {
            label: '企业名称',
            rules: { required: true, message: '请填写企业名称', trigger: 'change' },
          },
          attrs: {
            key: 'customer_name',
            value: '',
            class: 'form-control-ml',
            placeholder: '请填写企业名称',
            packageId,
          },
          listeners: {
            change: v => {
              this.clearWarehouse()
            },
            select: v => {
              this.selectWarehouse(v)
            },
          },
        },
        {
          tag: 'select',
          itemAttrs: {
            label: '企业类型',
            rules: [{ required: true, message: '请选择企业类型', trigger: 'change' }],
          },
          attrs: { key: 'org_class', clearable: false, options: enumCompanyType },
        },
        {
          tag: 'select',
          itemAttrs: {
            label: '行政级别',
            rules: [{ required: true, message: '请选择行政级别', trigger: 'change' }],
          },
          attrs: { key: 'lever_type', clearable: false, options: enumCompanyLevel },
        },
        {
          tag: SelectAddress,
          itemAttrs: {
            label: '经营地址',
            rules: { required: true, message: '请选择经营地址', trigger: 'change' },
          },
          attrs: {
            key: 'address_code',
            value: '',
            class: 'form-control-ml',
            autoTop: true,
            placeholder: '请选择',
          },
        },
        {
          tag: 'input',
          itemAttrs: { label: '详细地址' },
          attrs: { key: 'address_detail', value: '', placeholder: '请输入详细地址' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '联系人',
            rules: { required: true, message: '请输入联系人名称', trigger: 'blur' },
          },
          attrs: { key: 'corp_name', value: '', placeholder: '请输入联系人名称' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '身份证号',
            rules: [
              { required: true, message: '请输入身份证号', trigger: 'blur' },
              { validator: idCardValidator, message: '请输入正确的身份证号', trigger: 'blur' },
            ],
          },
          attrs: { key: 'id_card', value: '', placeholder: '请输入身份证号' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '联系电话',
            rules: [
              { required: true, message: '请输入手机号码', trigger: 'blur' },
              { validator: phoneValidator, message: '请输入正确的手机号码', trigger: 'blur' },
            ],
          },
          attrs: { key: 'mobile_no', value: '', placeholder: '联系电话' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '营业证号',
            rules: [
              { required: true, message: '请输入营业证号', trigger: 'blur' },
              { validator: registerNoValidator, message: '请输入正确的营业证号', trigger: 'blur' },
            ],
          },
          attrs: { key: 'register_no', value: '', placeholder: '请输入营业证号' },
          ifRender: model =>
            !['org_farmer', 'org_farm', 'org_large_breed'].includes(model.org_class),
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '生产环节',
            rules: { required: true, message: '请输入生产环节', trigger: 'blur' },
          },
          attrs: { key: 'parent_main_business', value: '', placeholder: '生产环节' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '主营业务',
            rules: { required: true, message: '请输入主营业务', trigger: 'blur' },
          },
          attrs: { key: 'main_business', value: '', placeholder: '请输入主营业务' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '产业规模',
            rules: { required: true, message: '请输入产业规模', trigger: 'blur' },
          },
          attrs: { key: 'scale', value: '', placeholder: '请输入产业规模' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '申请金额',
            rules: [{ required: true, message: '请输入申请金额', trigger: 'blur' }],
          },
          attrs: { key: 'apply_funds', value: '' },
          tagSlots: [{ name: 'append', slot: '万元' }],
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                max: 1000,
                fixed: 2,
                onMin: () => this.$message.error('不能小于0万元'),
                onMax: () => this.$message.error('不能大于1000万元'),
              },
            },
          ],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '申请年限',
            rules: [{ required: true, message: '请输入申请年限', trigger: 'blur' }],
          },
          attrs: { key: 'load_years', value: '' },
          tagSlots: [{ name: 'append', slot: '年' }],
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                max: 50,
                fixed: 2,
                onMin: () => this.$message.error('不能小于0年'),
                onMax: () => this.$message.error('不能大于50年'),
              },
            },
          ],
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '参与“9+3”重点县情况',
            rules: { required: true, message: '请输入', trigger: 'blur' },
          },
          attrs: { key: 'party_93_des', value: '', placeholder: '请输入参与“9+3”重点县情况' },
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '企业简介',
            rules: { required: true, message: '请输入企业简介', trigger: 'blur' },
          },
          attrs: { key: 'project_des', value: '', placeholder: '请输入企业简介' },
        },
      ],
      warehouse: {},
    }
  },
  computed: {
    ...mapGetters(['org', 'user']),
    tableId() {
      return this.formData.project_white_list_id
    },
    title() {
      if (this.isFinished) {
        return '完成申请表'
      }
      return (this.tableId ? '修改' : '新增') + '主体'
    },
    submitText() {
      return this.isFinished ? '完成申请表' : '保存'
    },
  },
  watch: {
    visible(val) {
      this.innerVisible = val
    },
    formData: {
      handler: function (val) {
        this.$nextTick(() => {
          const formRef = this.$refs.form
          if (formRef) {
            const { address } = val
            formRef.setAttrs('address_code', {
              class: `${address ? 'black-placeholder' : ''} form-control-ml`,
              placeholder: address || '请选择',
            })
          }
        })
      },
      immediate: true,
    },
  },
  created() {},
  methods: {
    handleClose() {
      this.$emit('update:visible', false)
    },
    onSuccess() {
      this.$emit('success', true)
    },
    clearWarehouse() {
      this.warehouse = {}
    },
    selectWarehouse(val) {
      this.warehouse = val
      fields.forEach(key => {
        this.$refs.form.setAttrs(key, { value: this.warehouse[key] })
      })
    },
    beforeSave(data) {
      data.project_type_id = this.packageId
      data.project_type_name = this.packageName
      data.is_finished_form = 1
      // 同步产业分类数据
      if (this.warehouse.datum_customer_info_warehouse_id) {
        ;['business_scope', 'bussiness_years'].forEach(key => {
          data[key] = this.warehouse[key]
        })
      }

      return data
    },
  },
}
</script>

<style lang="scss" scoped></style>
